package edu.ccut.saturn.manager.bundles.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import edu.ccut.saturn.component.IActionComponentInfo;
import edu.ccut.saturn.component.IComponentInfo;
import edu.ccut.saturn.component.ILogicComponentInfo;
import edu.ccut.saturn.component.INodeInfo;
import edu.ccut.saturn.component.impl.node.*;


public class ComponentGraphicInfoManager {
	private int minX = 0;
	private int minY = 0;

	private IComponentInfo componentInfo;

	/**
	 * @param componentInfo 组件信息
	 */
	public ComponentGraphicInfoManager(IComponentInfo componentInfo) {
		this.componentInfo = componentInfo;
	}

	/**
	 * 获得组件map信息
	 * 将IComponentInfo装配成用来图形化的map 
	 * 
	 * @return
	 */
	public Map<String, Object> getComponentGraphicInfoMap() {

		Map<String, Object> graphicInfoMap = new HashMap<String, Object>();

		graphicInfoMap.put(Constants.COMP_NAME, componentInfo.getName());
		graphicInfoMap
				.put(Constants.COMP_FULLkEY, componentInfo.getFullKey());
		graphicInfoMap.put(Constants.COMP_DESCRIPTION, componentInfo
				.getDescription());

		if (componentInfo instanceof ILogicComponentInfo) {
			ILogicComponentInfo logicComponentInfo = (ILogicComponentInfo) componentInfo;
			if (logicComponentInfo instanceof IActionComponentInfo) {
				graphicInfoMap.put(Constants.COMP_TYPE, "action");
			} else {
				graphicInfoMap.put(Constants.COMP_TYPE, "logic");
			}

			List<Map<String, String>> list = new ArrayList<Map<String, String>>();
			Map<String, String> startNodeInfoMap = getNodeInfoMap(getStartNodeInfo(logicComponentInfo));
			list.add(startNodeInfoMap);

			Set<String> nodeIdKeySet = logicComponentInfo.getNodeIdKeySet();
			Iterator<String> itr = nodeIdKeySet.iterator();

			while (itr.hasNext()) {
				String nodeIdKey = (String) itr.next();
				INodeInfo nodeInfo = logicComponentInfo.getNodeInfo(nodeIdKey);
				Map<String, String> nodeInfoMap = getNodeInfoMap(nodeInfo);
				list.add(nodeInfoMap);
			}

			modifyXYaxis(list);

			graphicInfoMap.put("nodeInfos", list);

		} else {

		}
		return graphicInfoMap;

	}

	/**
	 * 获得节点信息
	 * 
	 * @param nodeInfo
	 * @return
	 */
	public Map<String, String> getNodeInfoMap(INodeInfo nodeInfo) {
		Map<String, String> nodeInfoMap = new HashMap<String, String>();

		nodeInfoMap.put(Constants.COMP_NODE_ID, nodeInfo.getId());
		nodeInfoMap.put(Constants.COMP_NODE_NAME, nodeInfo.getName());
		nodeInfoMap.put(Constants.COMP_NODE_NEXT_ID, nodeInfo.getNextId());
		nodeInfoMap.put(Constants.COMP_NODE_DESCRIPTION, nodeInfo
				.getDescription());
		nodeInfoMap.put(Constants.COMP_NODE_X, nodeInfo.getX());
		nodeInfoMap.put(Constants.COMP_NODE_Y, nodeInfo.getY());

		int x = Integer.parseInt(nodeInfo.getX());
		int y = Integer.parseInt(nodeInfo.getY());

		if (x < minX) {
			minX = x;
		}
		if (y < minY) {
			minY = y;
		}
		nodeInfoMap.put(Constants.COMP_NODE_TYPE, nodeInfo.getNodeType());
		if (nodeInfo instanceof DecisionNodeInfo) {
			DecisionNodeInfo decisionNodeInfo = (DecisionNodeInfo) nodeInfo;
			nodeInfoMap.put(Constants.COMP_NODE_THEN_ID, decisionNodeInfo
					.getThen());
			nodeInfoMap.put(Constants.COMP_NODE_ELSE_ID, decisionNodeInfo
					.getElseId());
		} else if (nodeInfo instanceof BaseComponentNodeInfo) {
			BaseComponentNodeInfo baseComponentNodeInfo = (BaseComponentNodeInfo) nodeInfo;
			nodeInfoMap.put(Constants.COMP_NODE_KEY, baseComponentNodeInfo
					.getKey());
			nodeInfoMap.put(Constants.COMP_NODE_EXCEPTION_ID,
					baseComponentNodeInfo.getExceptionId());
		} else if (nodeInfo instanceof SubActionNodeInfo) {
			SubActionNodeInfo subActionNodeInfo = (SubActionNodeInfo) nodeInfo;
			nodeInfoMap
					.put(Constants.COMP_NODE_KEY, subActionNodeInfo.getKey());
		}
		return nodeInfoMap;
	}

	/**
	 * 获得开始节点信息
	 * 
	 * @param logicComponentInfo
	 * @return
	 */
	private INodeInfo getStartNodeInfo(ILogicComponentInfo logicComponentInfo) {
		return logicComponentInfo.getStartNodeInfo();
	}

	/**
	 * 修改xy坐标值，当x或y存在负数的时候,在原坐标上减去最大的负坐标值，使全部坐标都变成正值
	 * 
	 * @param list
	 */
	private void modifyXYaxis(List<Map<String, String>> list) {
		if (minX >= 0 && minY >= 0) {
			return;
		} else {
			for (Map<String, String> nodeInfoMap : list) {
				if (minX < 0) {
					int x = Integer.parseInt(nodeInfoMap
							.get(Constants.COMP_NODE_X))
							- minX;
					nodeInfoMap.put(Constants.COMP_NODE_X, String.valueOf(x));
				}
				if (minY < 0) {
					int y = Integer.parseInt(nodeInfoMap
							.get(Constants.COMP_NODE_Y))
							- minY;
					nodeInfoMap.put(Constants.COMP_NODE_Y, String.valueOf(y));
				}
			}
		}
	}

}
